#include <stdio.h>
#include "parser.h"
#include "scan.h"

static tokentype_t token;

void match(tokentype_t expected) {
    if (expected == token) {
        token = get_token();
    } else {
        printf(" token expected");
    }
}

/*
 * declaration -> var-declaration | func-declaration | func-definition
 */
static struct treenode_t* declare() {
    struct treenode_t* p = NULL;
    match(token);
    match(ID);

    if (token == LPAREN) {
    } else {

    }

    return p;
}

/*
 * declaration-list -> declaration-list declaration |
 *                     declaration
 * 消除左递归,declaration之间互为兄弟节点
 *
 */
static struct treenode_t* decl_list() {
    struct treenode_t* p = declare();
    struct treenode_t* q = p;
    while (token != ENDFILE) {
        q->next = declare();
        q = q->next;
    }
    return p;
}

/* program -> declaration-list */
struct treenode_t* parse() {
  token = get_token();
  return decl_list();
}
